// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.ppe;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.Destination;
import software.amazon.awssdk.services.ses.model.Content;
import software.amazon.awssdk.services.ses.model.SendEmailRequest;
import software.amazon.awssdk.services.ses.model.SesException;
import software.amazon.awssdk.services.ses.model.Message;
import software.amazon.awssdk.services.ses.model.Body;
import java.util.Set;

public class SendEmail {

        public void sendMsg(Set<String> unqiueKeys) {

                Region region = Region.US_EAST_1;
                SesClient client = SesClient.builder()
                                .region(region)
                                .build();

                String sender = "<Enter the sender email address>";
                String recipient = "<Enter the recipient email address>";

                // Set the HTML body.
                String bodyHTML = "<html> <head></head> <body><p> The following images contains PPE gear " +
                                "<ol> ";

                // Persist the data into a DynamoDB table.
                for (String myKey : unqiueKeys) {
                        bodyHTML = bodyHTML + "<li> " + myKey + "</li>";

                }

                bodyHTML = bodyHTML + "</ol></p></body></html>";
                Destination destination = Destination.builder()
                                .toAddresses(recipient)
                                .build();

                Content content = Content.builder()
                                .data(bodyHTML)
                                .build();

                Content sub = Content.builder()
                                .data("PPE Information")
                                .build();

                Body body = Body.builder()
                                .html(content)
                                .build();

                Message msg = Message.builder()
                                .subject(sub)
                                .body(body)
                                .build();

                SendEmailRequest emailRequest = SendEmailRequest.builder()
                                .destination(destination)
                                .message(msg)
                                .source(sender)
                                .build();

                try {
                        System.out.println("Attempting to send an email through Amazon SES "
                                        + "using the AWS SDK for Java...");
                        client.sendEmail(emailRequest);

                } catch (SesException e) {
                        System.err.println(e.awsErrorDetails().errorMessage());
                        System.exit(1);
                }
        }
}
